package pojo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import besuper2.Dao;

public class Player {
	public int ID;
	public int score;
	public String username = null;
	public String password = null;
	public String nickname = null;
	public List<Question> questions = new ArrayList<>();
	public List<Answer> answers = new ArrayList<>();
	
	public Player(String username, String password, String nickname) {
		this.username = username;
		this.score = this.getScore();
		this.nickname = nickname;
		this.password = password;
	}
	
	public int getScore() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		String sql = "SELECT * from `besuper`.`player` where username = ? and password = ?";
		try {
			conn = Dao.getConnection();
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, this.getUsername());
			stmt.setString(2, this.getPassword());

			rs = stmt.executeQuery();

			while (rs.next()) {
				score = rs.getInt("score");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dao.clean(conn, stmt, rs);
		}
		
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public List<Question> getQuestions() {
		List<Question> questions = new ArrayList<>();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		String sql = "SELECT * from `besuper`.`question` where player_id = ?";
		try {
			conn = Dao.getConnection();
			stmt = conn.prepareStatement(sql);

			stmt.setInt(1, this.getID());

			rs = stmt.executeQuery();

			while (rs.next()) {
				int id = rs.getInt("id");
				String value = rs.getString("value");
				int credit = rs.getInt("credit");
				
				Question question = new Question(id, value, this.getID(), credit);
				questions.add(question);
			}
			return questions;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dao.clean(conn, stmt, rs);
		}
	
		return questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}

	public List<Answer> getAnswers() {
		List<Answer> answers = new ArrayList<>();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		String sql = "SELECT * from `besuper`.`answer` where player_id = ?";
		try {
			conn = Dao.getConnection();
			stmt = conn.prepareStatement(sql);

			stmt.setInt(1, this.getID());

			rs = stmt.executeQuery();

			while (rs.next()) {
				int id = rs.getInt("id");
				String value = rs.getString("value");
				
				Answer answer = new Answer(id, value, this.getID());
				answers.add(answer);
			}
			return answers;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dao.clean(conn, stmt, rs);
		}
	
		return answers;
	}

	public void setAnswers(List<Answer> answers) {
		this.answers = answers;
	}

	public int getID() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		String sql = "SELECT * from `besuper`.`player` where username = ? and password = ?";
		try {
			conn = Dao.getConnection();
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, this.getUsername());
			stmt.setString(2, this.getPassword());

			rs = stmt.executeQuery();

			while (rs.next()) {
				ID = rs.getInt("id");
			}
			return ID;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Dao.clean(conn, stmt, rs);
		}
		
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getNickname() {
		return nickname;
	}
	
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	@Override
	public String toString() {
		return "Player [username=" + username + ", password=" + password + ", nickname=" + nickname + "]";
	}

}
